package org.farng.mp3.object;

import org.farng.mp3.TagUtility;


/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.7 $
 */
public class ObjectNumberVariableLength extends AbstractMP3Object {
    /** DOCUMENT ME! */
    int minLength = 1;

    /**
     * Creates a new ObjectNumberVariableLength object.
     *
     * @param identifier DOCUMENT ME!
     * @param minimumSize DOCUMENT ME!
     */
    public ObjectNumberVariableLength(String identifier, int minimumSize) {
        this.identifier = identifier;

        if (minimumSize > 0) {
            this.minLength = minimumSize;
        }
    }

    public ObjectNumberVariableLength(ObjectNumberVariableLength copyObject) {
        super(copyObject);
        this.minLength = copyObject.minLength;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getMaximumLenth() {
        return 8;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getMinimumLength() {
        return minLength;
    }

    /**
     * DOCUMENT ME!
     *
     * @param minimumSize DOCUMENT ME!
     */
    public void setMinimumSize(int minimumSize) {
        if (minimumSize > 0) {
            this.minLength = minimumSize;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        if (value == null) {
            return 0;
        } else {
            int  current;
            long temp = TagUtility.getWholeNumber(value);
            int  size = 0;

            for (int i = 1; i <= 8; i++) {
                current = (byte) temp & 0xFF;

                if (current != 0) {
                    size = i;
                }

                temp >>= 8;
            }

            return (minLength > size) ? minLength : size;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectNumberVariableLength) == false) {
            return false;
        }

        ObjectNumberVariableLength object = (ObjectNumberVariableLength) obj;

        if (this.minLength != object.minLength) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param arr DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readByteArray(byte[] arr, int offset) {
        if (arr == null) {
            throw new NullPointerException("Byte array is null");
        }

        if ((offset < 0) || (offset >= arr.length)) {
            throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length);
        }

        long lvalue = 0;

        for (int i = offset; i < arr.length; i++) {
            lvalue <<= 8;
            lvalue += arr[i];
        }

        value = new Long(lvalue);
    }

    /**
     * DOCUMENT ME!
     *
     * @param str DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String str, int offset) {
        if (str == null) {
            throw new NullPointerException("Number string is null");
        }

        if ((offset < 0) || (offset >= str.length())) {
            throw new IndexOutOfBoundsException("Offset to number string is out of bounds: offset = " + offset + ", string.length()" + str.length());
        }

        value = Long.getLong(str.substring(offset));
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        if (value == null) {
            return "";
        } else {
            return value.toString();
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public byte[] writeByteArray() {
        int    size = getSize();
        byte[] arr;

        if (size == 0) {
            arr = new byte[0];
        } else {
            long temp = TagUtility.getWholeNumber(value);
            arr = new byte[size];

            for (int i = size - 1; i >= 0; i--) {
                arr[i] = (byte) (temp & 0xFF);
                temp >>= 8;
            }
        }

        return arr;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        if (value == null) {
            return "";
        } else {
            return value.toString();
        }
    }
}